<?php
    /*
    Plugin Name: Google Analytics
    Plugin URI: http://shopos3.com/
    Version: 1.0
    */

    defined('_VALID_OS') or die('Direct Access to this location is not allowed.');

    if ( get_option ('check_jquery') == 'true' )
    {
        if ( is_page('checkout_success') )
        {
            add_action('head', 'head_analytics_checkout_success'); 
        }
        else
        {
            add_action('head', 'head_analytics');
        }
    }

    function analytics_install ()
    {
        add_option('check_google_analytics', 'true', 'radio', "array('true','false')");
        add_option('google_analytics_id', 'UA-XXXXXX-X');
        add_option('google_analytics_lang', 'ru');
    }

    function head_analytics()
    { 
        $google_analytics_id = get_option('google_analytics_id');
        $google_analytics_id = trim($google_analytics_id);

        if ( $google_analytics_id != 'UA-XXXXXX-X')
        {
            _e('<!-- Google.Analytics -->');
            _e('<script type="text/javascript">
            var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
            </script>  ');

            _e('<script type="text/javascript">
            var pageTracker = _gat._getTracker("'.$google_analytics_id.'");
            pageTracker._initData();
            pageTracker._trackPageview();
            </script>');
            _e('<!-- /Google.Analytics -->');

        }  
    }

    function head_analytics_checkout_success()
    {
        $google_analytics_id = get_option('google_analytics_id');
        $google_analytics_id = trim($google_analytics_id);

        if ( $google_analytics_id != 'UA-XXXXXX-X')
        {
            include('analytics.func.php');

            _e('<!-- Google.Analytics -->');

            _e('<script type="text/javascript">
            var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
            document.write(unescape("%3Cscript src=\'" + gaJsHost + "google-analytics.com/ga.js\' type=\'text/javascript\'%3E%3C/script%3E"));
            </script>  ');

            _e('<script type="text/javascript">
            var pageTracker = _gat._getTracker("' . $google_analytics_id . '");
            pageTracker._initData();
            pageTracker._trackPageview();
            pageTracker._addTrans(
            ' . $transaction_string . ');
            ' . $item_string . '
            pageTracker._trackTrans();
            </script>');

            _e('<!-- /Google.Analytics -->');
        }
    }

?>